"""
演示Python pymysql库的基础操作
"""
from pymysql import Connection

# 构建到MySQL数据库的链接
connection = Connection(
    host="localhost",  # 主机名（IP）
    port=3306,  # 端口
    user="root",  # 账户
    password="123456",  # 密码
    autocommit=True  # 设置自动提交
)

# print(connection.get_server_info())
# 执行非查询性质SQL
cursor = connection.cursor()  # 获取到游标对象
# 选择数据库
connection.select_db("test1")
# 执行sql
# cursor.execute("create table test_pymysql(id int)")  # 建表
cursor.execute("select * from user")  # 查
results = cursor.fetchall()  # 获取查询结果
for r in results:
    print(r)
# 关闭链接
connection.close()
